package proyecto.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import proyecto.excepcion.DAOExcepcion;
import proyecto.excepcion.LoginExcepcion;
import proyecto.modelo.Paciente;
import proyecto.service.PacienteService;

public class LoginPacienteController extends SimpleFormController {

    private PacienteService pacienteService;

    public PacienteService getPacienteService() {
        return pacienteService;
    }

    public void setPacienteService(PacienteService pacienteService) {
        this.pacienteService = pacienteService;
    }

    @Override
    public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = null;

        String u = request.getParameter("txtlogin");
        String p = request.getParameter("txtpassword");
        
        System.out.println("Login Paciente Controller: " + u);
        System.out.println("Password Paciente Controller: " + p);
        
        try {
            Paciente vo = pacienteService.validar(u, p);
            HttpSession session = request.getSession();
            session.setAttribute("PACIENTE_ACTUAL", vo);
            mv = new ModelAndView("redirect:/pacientes_principal.htm");
        } catch (DAOExcepcion ex) {
            System.err.println(ex.toString());
            mv = new ModelAndView("error");
        } catch (LoginExcepcion ex) {
            System.out.println(ex.toString());
            mv = new ModelAndView("error");
        }
        return mv;
    }
}
